var courseLib = new Vue({
    el: "#rootDiv",
    data: {
        h: headers,
        courses: [],
        cname: '',
        categories: [],
        tags: [],
        statuses: ["上架/发布", "下架", "草稿箱"],
        selectCategory: 0,
        selectTag: 0,
        selectStatus: 0,
        totalPage: 0,
        pageSize: 5,
        pageCode: 1,
        tagCondition: '',
        condition: {},
        prev: false,
        next: false,
    },
    mounted() {
        axios({
            method: "get",
            url: "/course/courseCategory/all",
            headers: this.h
        }).then(res => {
            console.info("courseCategory-res:")
            console.info(res)
            if (res.data.code === 200) {
                this.categories = res.data.data;
            }
        }).catch(err => {
            console.info(err)
        });
        this.queryCoachesByCondition();
        axios({
            method: "get",
            url: "/course/allTags",
            headers: this.h
        }).then(res => {
            console.info("allTags-res:")
            console.info(res)
            if (res.data.code === 200) {
                this.tags = res.data.data;
            }
        }).catch(err => {
            console.info(err)
        });
    },
    methods: {
        clear: function () {
            this.cname = '';
            this.selectCategory = 0;
            this.selectStatus = 0;
            this.pageSize = 5;
            this.pageCode = 1;
            this.tagCondition = '';
            this.queryCoachesByCondition();
        },
        changeStatus: function () {
            this.selectStatus += 1;
            console.info("状态id：" + this.selectStatus);
        },
        queryThisPage: function (n) {
            console.info("n:" + n);
            this.pageCode = n;
            this.condition = {
                "cname": this.cname,
                "category": this.selectCategory,
                "cstatus": this.selectStatus,
                "tagCondition": this.tagCondition,
                "index": this.pageCode,
                "pageSize": this.pageSize
            };
            axios({
                method: "post",
                url: "/course/condition",
                data: this.condition,
                headers: this.h
            }).then(response => {
                console.info("course:" + response);
                console.info(response.data);
                let pageBean = response.data.data;
                this.totalPage = pageBean.totalPage;
                console.info(response.data.data.data);
                this.courses = response.data.data.data;
                if (1 === this.totalPage) {
                    this.next = true;
                }
            })
                .catch(err => {
                    // console.info(err);
                });

            if (n === 1) {
                this.prev = true;
                console.info("prev" + this.prev);
            } else {
                this.prev = false;
            }
            if (n === this.totalPage) {
                this.next = true;
                console.info("next" + this.next);
            } else {
                this.next = false;
            }
            // this.pageCode=n;
            // if (this.pageCode===n){
            // 	return "active";
            // }
        },//queryFunction end
        //提交条件
        queryCoachesByCondition: function () {
            this.condition = {
                "cname": this.cname,
                "category": this.selectCategory,
                "cstatus": this.selectStatus,
                "tagCondition": this.tagCondition,
                "index": this.pageCode,
                "pageSize": this.pageSize
            };
            this.queryThisPage(1);
        },
        //前后按钮翻页
        forwardOne: function () {
            //如果被禁用就不能操作
            if (this.prev === true) {
                return;
            }
            this.pageCode = this.pageCode - 1;
            let num = this.pageCode;
            this.queryThisPage(num);
        },
        backOne: function () {
            //如果被禁用就不能操作
            if (this.next === true) {
                return;
            }
            this.pageCode = this.pageCode + 1;
            let num = this.pageCode;
            this.queryThisPage(num);
        }
    }
});